﻿#include "DpkCommon.h"
#include "Crc32.h"
#include "CCFileUtils.h"


void Luck( char* pData,int nLen,LPCSTR szKey )
{
	size_t nKeyLen=strlen(szKey);
	size_t nIndex=0;
	for (int i=0;i<nLen;++i)
	{
		if (nIndex>=nKeyLen)
		{
			nIndex=0;
		}
		pData[i]^=szKey[nIndex];

		++nIndex;
	}
}


DPK_READ_ERROR ReadDpk( LPCSTR dpkName,LPCSTR dpkKey,DpkNodeMap & dataArr )
{
	int nRealSize=0;
	FILE* pf=NULL;

	//打开文件
	pf=fopen(dpkName,"rb");
	if (!pf)
	{
		return DPK_FILE_OPEN_ERROR;
	}

	//判断文件头
	char head[DEBUG_PDK_HEAD_LENGTH];
	nRealSize=fread(head,sizeof(char),DEBUG_PDK_HEAD_LENGTH,pf);
	ASSERT(nRealSize==DEBUG_PDK_HEAD_LENGTH);
	if(strcmp(head,DEBUG_PDK_HEAD))
	{
		return DPK_FILE_HEAD_ERROR;
	}

	//验证crc
	unsigned long rcrc=0;
	nRealSize=fread(&rcrc,sizeof(rcrc),1,pf);
	ASSERT(1== nRealSize);
	int nLen=strlen(dpkKey)*sizeof(char);
	CCrc32 crc32;
	unsigned long crc = crc32.CalculateCrc32((const unsigned char*)dpkKey,nLen);
	if (rcrc!=crc)
	{
		return DPK_CRC_ERROR;
	}
	char szPathName[DEBUG_PDK_PATH_MAX_LENGTH]={};
	SDpkNodeMapData data;
	while(true)
	{
		//读取路径信息
		nRealSize=fread(szPathName,sizeof(char),DEBUG_PDK_PATH_MAX_LENGTH,pf);
		if (nRealSize==0)
		{
			break;
		}
		ASSERT(nRealSize == DEBUG_PDK_PATH_MAX_LENGTH);
		//读取大小信息
		nRealSize=fread(&data.nSize,sizeof(data.nSize),1,pf);
		ASSERT(nRealSize == 1);
		ASSERT(data.nSize>=0);
		//读取数据
		data.pData=new char[data.nSize]();
		nRealSize=fread(data.pData,sizeof(char),data.nSize,pf);
		ASSERT(nRealSize == data.nSize);

		//解密
		Luck((char*)data.pData,data.nSize,dpkKey);

		dataArr.insert(DpkNodeMapType(szPathName,data));
	}

	fclose(pf);

	return DPK_SUCCESS;
}

DPK_READ_ERROR ReadDpkWithData( LPCSTR pData,int nSize,LPCSTR dpkKey,DpkNodeMap & dataArr )
{
	int nOffset=0;

	//判断文件头
	char head[DEBUG_PDK_HEAD_LENGTH];
	memcpy(head,pData+nOffset,DEBUG_PDK_HEAD_LENGTH);
	nOffset+=DEBUG_PDK_HEAD_LENGTH;
	if(strcmp(head,DEBUG_PDK_HEAD))
	{
		return DPK_FILE_HEAD_ERROR;
	}

	//验证crc
	unsigned long rcrc=0;
	memcpy(&rcrc,pData+nOffset,sizeof(rcrc));
	nOffset+=sizeof(rcrc);
	int nLen=strlen(dpkKey)*sizeof(char);
	CCrc32 crc32;
	unsigned long crc = crc32.CalculateCrc32((const unsigned char*)dpkKey,nLen);
	if (rcrc!=crc)
	{
		return DPK_CRC_ERROR;
	}

	char szPathName[DEBUG_PDK_PATH_MAX_LENGTH]={};
	SDpkNodeMapData data;

	while(true)
	{
		if (nOffset+DEBUG_PDK_PATH_MAX_LENGTH>=nSize)
		{
			break;
		}
		//读取路径信息
		memcpy(szPathName,pData+nOffset,DEBUG_PDK_PATH_MAX_LENGTH);
		nOffset+=DEBUG_PDK_PATH_MAX_LENGTH;

		//读取大小信息
		memcpy(&data.nSize,pData+nOffset,sizeof(data.nSize));
		nOffset+=sizeof(data.nSize);

		//读取数据
		data.pData=new char[data.nSize]();
		memcpy(data.pData,pData+nOffset,data.nSize);
		nOffset+=data.nSize;

		//解密
		Luck((char*)data.pData,data.nSize,dpkKey);

		std::string strkey=cocos2d::CCFileUtils::sharedFileUtils()->getRootPath()+szPathName;
		
		dataArr.insert(DpkNodeMapType(strkey,data));
	}

	return DPK_SUCCESS;
}




